import React, {PureComponent} from 'react'

/**
 * 解析从控件返回的xml字符串
 */
function loadXML(xmlString) {
    if (!g_xmlActive) {
        return;
    }
    g_xmlActive.loadXML(xmlString);
    if (0 === g_xmlActive.parseError.errorCode) {
        return g_xmlActive;
    }
    else {
        alert("xml解析错误:" + g_xmlActive.parseError.reason);
        return null;
    }
}


//全局变量---start

let g_imosActivePlayer = null;
let g_xmlActive = null;

//全局变量---end


//================>功能函数---start
//检查控件
function InitPage() {
    g_imosActivePlayer = document.all.h3c_IMOS_ActiveX;
    if (!g_imosActivePlayer) {
        alert("未安装控件，请先安装后再使用本页面");
    }

    let xmldoc;
    try {
        xmldoc = new ActiveXObject("Microsoft.XMLDOM");
        if (!xmldoc) {
            xmldoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
        }
    }
    catch (e) {
    }
    g_xmlActive = xmldoc;
    if (!g_xmlActive) {
        alert("xml解析器获取错误，将导致某些功能不可用");
    }
    else {
        g_xmlActive.async = "false";
    }
}

//登录
function DoLogin() {
    const serverIP = '10.88.112.36'
    const userName = 'show'
    const passWd = 'show'
    g_imosActivePlayer.IMOSAX_InitOCX(serverIP, "8800", userName, passWd, 1);

    //获取用户信息
    const retStr = g_imosActivePlayer.IMOSAX_GetUserLoginInfo();
    const userObj = loadXML(retStr);
    g_imosActivePlayer.IMOSAX_ChangeLayout(1);

}


//退出登录
function DoLogout() {
    if (g_imosActivePlayer) {
        const flag = g_imosActivePlayer.IMOSAX_UnregOCX();
        if (0 !== flag) {
            //暂时不提示
        }
    }
}

//启动实况
function DoStartPlay(cameraId) {
    const flag = g_imosActivePlayer.IMOSAX_StartFrameLive(1, cameraId);
    if (0 === flag) {
        // alert("实况播放成功");
    }
    else {
        alert("播放实况失败，错误码：" + flag);
    }
}

//释放实况
function DoStopPlay() {
    const flag = g_imosActivePlayer.IMOSAX_StopFramelive(1);
    if (0 === flag) {
        // alert("停止实况成功");
    }
    else {
        alert("停止实况失败，错误码：" + flag);
    }
}

class VideoPlayer extends PureComponent {
    constructor() {
        super()
    }


     componentWillUnmount() {
        DoLogout()
        window.removeEventListener('beforeunload', DoLogout)
    }


     componentDidMount() {
        InitPage()
        DoLogin()
        DoStartPlay('63051b8dH3C_6621EX205CIN_7_001X0')
        window.addEventListener('beforeunload', DoLogout)
    }

    videoElement = `<object classID="clsid:067A4418-EBAC-4394-BFBE-8C533BA6503A" id="h3c_IMOS_ActiveX" events="true"
                        height="100%"
                        width="100%">
                </object>`

    render() {
        return (
            <div className="video-container">
                <div style={{width: '100%', height: '100%'}} dangerouslySetInnerHTML={{__html: this.videoElement}}>

                </div>


                {/*<div className="video-all">*/}
                {/*<div class="face1">*/}
                {/*<iframe id='iframebar' src="about:blank"*/}
                {/*frameBorder='0'/>*/}
                {/*test1*/}
                {/*</div>*/}
                {/*</div>*/}

            </div>
        )
    }
}

export default VideoPlayer